﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using Filter;
    using System;
    using System.Drawing;
    using System.Globalization;
    using System.Windows.Forms;

    /// <summary>Filter specified properties form.</summary>
    public partial class ColoredSobelForm : Form, IOptionsForm
    {
        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        private readonly SobelEdgeDetector sobel;
        private readonly ColoredSobelEdgeDetector coloredSobel;
        private bool drawInImage;        
        private readonly SimpleDilatation dilatation;

        /// <summary>
        /// Initializes a new instance of the <see cref="ColoredCannyForm"/> class.
        /// </summary>
        /// <param name="processor">The SBIP processor.</param>
        public ColoredSobelForm(Processor processor)
        {
            InitializeComponent();
            sobel = AOF.Init(processor, this, gbProcess,
                                new SobelEdgeDetector()) as SobelEdgeDetector;

            coloredSobel = new ColoredSobelEdgeDetector();
            dilatation = new SimpleDilatation {Diameter = 1};
            drawInImage = false;
        }

        /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            Bitmap b;
            if (drawInImage)
            {
                SobelEdgeMarker marker = new SobelEdgeMarker
                {
                    SobelEdgeDetector = sobel,
                    SimpleDilatation = dilatation
                };
                b = marker.Apply(Processor.OriginalImage);
            }
            else
            {
                b = rbColored.Checked ? 
                                coloredSobel.Apply(Processor.OriginalImage) : 
                                sobel.Apply(Processor.OriginalImage);

                if (dilatation.Diameter > 1)
                {
                    b = dilatation.Apply(b);
                }
            }

            string sMode = rbNormal.Checked ? Program.L.Text[28] :
                (rbColored.Checked ? Program.L.Text[29] : Program.L.Text[30]);
            string sDrawInImage = rdBtnNoDrawInImage.Checked ? Program.L.Text[34] : 
                Program.L.Text[35];
            string sUseThreshold = cbUseTreshold.Checked ?
                (Program.L.Text[35] + " (" + lbThresholdNumber.Text + ")") : 
                Program.L.Text[34];

            string s = Program.L.Text[40] + Program.L.Text[25] + ": " +
                Program.L.Text[27] + "=" + sMode + "; " +
                Program.L.Text[32] + "=" + sUseThreshold + "; " +
                Program.L.Text[33] + "=" + sDrawInImage + "; " +
                Program.L.Text[37] + "=" + lbDiameter.Text;
            Processor.Change(b, s);            
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[25];
            tabctrlOptions.TabPages[0].Text = Program.L.Text[26];
            gbDrawModes.Text = Program.L.Text[27];
            rbNormal.Text = Program.L.Text[28];
            rbColored.Text = Program.L.Text[29];
            rbBlackWhite.Text = Program.L.Text[30];
            gbThreshold.Text = Program.L.Text[31];
            cbUseTreshold.Text = Program.L.Text[32];
            gbDrawInImage.Text = Program.L.Text[33];
            rdBtnNoDrawInImage.Text = Program.L.Text[34];
            rdBtnYesDrawInImage.Text = Program.L.Text[35];
            gbLineThickness.Text = Program.L.Text[36];
            lbDiameterText.Text = Program.L.Text[37];
        }

        #region Edge Drawing tab control events
        private void RbNormalCheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                // filter.IsColored = false;
                sobel.BlackWhite = false;
                Processor.ApplyFilterRTP();
            }
        }

        private void RbColoredCheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                // filter.IsColored = true;
                rdBtnNoDrawInImage.Checked = true;
                sobel.BlackWhite = false;
                Processor.ApplyFilterRTP();
            }            
        }

        private void RbBlackWhiteCheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                // filter.IsColored = false;
                sobel.BlackWhite = true;
                cbUseTreshold.Checked = true;
                Processor.ApplyFilterRTP();
            }
        }

        private void RdBtnYesDrawInImageCheckedChanged(object sender, EventArgs e)
        {
            drawInImage = rdBtnYesDrawInImage.Checked;
            if (rdBtnYesDrawInImage.Checked)
            {
                rbNormal.Checked = true;
            }
            Processor.ApplyFilterRTP();
        }

        private void TrBarDiameterValueChanged(object sender, EventArgs e)
        {
            dilatation.Diameter = (byte)(trBarDiameter.Value);// * 2 + 1);
            lbDiameter.Text = dilatation.Diameter.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void CbUseTresholdCheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)sender).Checked)
            {
                trBarThreshold.Visible = true;
                TrBarThresholdValueChanged(sender, e);
            }
            else
            {
                sobel.Threshold = 0;
                coloredSobel.Threshold = 0;
                trBarThreshold.Visible = false;
                if (rbBlackWhite.Checked)
                {
                    rbNormal.Checked = true;
                }
                else
                {
                    Processor.ApplyFilterRTP();
                }                
            }
        }

        private void TrBarThresholdValueChanged(object sender, EventArgs e)
        {
            sobel.Threshold = (byte)trBarThreshold.Value;
            coloredSobel.Threshold = (byte)trBarThreshold.Value;
            lbThresholdNumber.Text = trBarThreshold.Value.ToString(
                                                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        #endregion Edge Drawing tab control events

        private void BtnProcessClick(object sender, EventArgs e)
        {
            try
            {
                ApplyFilter();   
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }
    }
}
